/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.UI;

/**
 *
 * @author meydlo
 */
import java.util.ArrayList;
import javaapplication6.ControlLayer.WorkerControl;
import javaapplication6.DomainLayer.Worker;
public class LoginUI {

    private Listener listener=new Listener();
    private WorkerControl workerControl;
    private int num=0;
    private Worker worker=null;

    public LoginUI()
    {
        workerControl=new WorkerControl();
    }
    
    public Worker logIn()
    {
        boolean loggedIn=false;
        do{
            System.out.println("please write ID:");
            num=listener.getLineInt();

                worker=workerControl.findWorker(num);
                if(worker!=null){
                    loggedIn=worker.getPassword().equals(getPassword());
                    if(loggedIn){
                        worker=workerControl.findWorker(num);
                        return workerControl.findWorker(num);
                    }
                    else{
                        System.out.println("wrong password");
                    }
               
            }
        }while(loggedIn==false);
        return null;
    }


    public void createDefaultUsers()
    {
        workerControl.createWorker("salesman", "null", "null",0,1,"1");
        workerControl.createWorker("admin", "null", "null",0,2,"2");
        workerControl.createWorker("manager", "null", "null",0,3,"3");

        ArrayList<Worker> workers=workerControl.getWorkerList();

        //to set default users (id (1,2,3)
        for(int i=0; i<workers.size(); i++){
            Worker employee=workers.get(i);
            long empId=employee.getId();
            employee.setId(empId-3);
        }
        
    }


    public String getPassword()
    {
        System.out.println("write password");
        String password=listener.getLineString();
        return password;
    }


    
}
